<?php

// Acceso restringido
defined('JL') or die('Acceso restringido');

/**
 * PhotosAdminController
 * 
 * @package JL Panel
 * @author JLQ
 * @copyright 2009
 * @version 1.0
 * @access public
 */
class PhotosAdminController extends JLControllerBase {

    private $config = null;
    private $path = "PhotosAdmin";
    private $title = "Système d'Actualisation CSME > PhotosAdmin";
    private $data = null;
    private $bool_admin = 1;
    private $galleryPath = "galeries";

    /**
     * LiensController::getGallery()
     * 
     * @return
     */
    function getGallery() {
        $id = $_POST['_id'];

        require JLPATH_MODELS . DS . "GalleryModel.php";
        $galleries = new GalleryModel();
        $gallery = $galleries->getGalleryById($id);

        $var_title_gallery = $gallery['var_title_gallery'];
        $var_path_gallery = $gallery['var_path_gallery'];
        $int_id_state = $gallery['int_id_state'];

        echo $var_title_gallery . "_%_" . $var_path_gallery . "_%_" . $int_id_state;
    }

    /**
     * LiensController::updateLiens()
     * 
     * @return
     */
    function updateGallery() {
        $id = $_POST['_id'];
        $var_title_gallery = $_POST['_var_title_gallery'];
        $int_id_sections_view = $_POST['_int_id_sections_view'];
        $int_id_state = $_POST['_int_id_state'];

        require JLPATH_MODELS . DS . "GalleryModel.php";
        $galleries = new GalleryModel();

        $_true = $galleries->updateGallery($id, $var_title_gallery, $int_id_sections_view, $int_id_state, $this->bool_admin);

        if ($_true)
            echo "Données sauvegarder avec succès.";
        else
            echo "";
    }

    /**
     * PhotosController::insertGallery()
     * 
     * @return
     */
    function insertGallery() {
        $var_title_gallery = $_POST['_var_title_gallery'];
        $int_num_photos = 0;
        $int_id_sections_view = $_POST['_int_id_sections_view'];
        $int_id_state = $_POST['_int_id_state'];

        $jl = new JLTools();
        $namedir = "gallerie-" . $jl->RandomNumerosLetras(12, "ambos");
        $var_path_gallery = $namedir;

        require JLPATH_MODELS . DS . "GalleryModel.php";
        $galleries = new GalleryModel();
        $_true = $galleries->insertGallery($var_title_gallery, $int_num_photos, $var_path_gallery, $int_id_sections_view, $int_id_state, $this->bool_admin);

        if ($_true != 0) {
            $jl->MakeDir(JLPATH_WEB_SITE . DS . $this->galleryPath . DS . $namedir);
            $jl->MakeDir(JLPATH_WEB_SITE . DS . $this->galleryPath . DS . $namedir . DS . "view");
            $jl->MakeDir(JLPATH_WEB_SITE . DS . $this->galleryPath . DS . $namedir . DS . "thumb");
            echo $_true;
        }
        else
            echo 0;
    }

    /**
     * LiensController::deleteGallery()
     * 
     * @return
     */
    function deleteGallery() {
        $ids = $_POST['_ids'];
        $ides = explode(",", $ids);
        require JLPATH_MODELS . DS . "GalleryModel.php";
        require JLPATH_MODELS . DS . "ImagesModel.php";
        $Images = new ImagesModel();
        $galleries = new GalleryModel();
        $jl = new JLTools();

        $i = 0;
        foreach ($ides as $id) {
            $gallerie = $galleries->getGalleryById($id);
            $namedir = $gallerie['var_path_gallery'];
            $_true = $galleries->deleteGallery($id);
            if (!$_true)
                break;

            $Images->deleteImagesByIdGallery($id);
            $jl->DeleteDirectory(JLPATH_WEB_SITE . DS . $this->galleryPath . DS . $namedir);

            if (!empty($gallerie['var_image_reference'])) {
                @unlink(JLPATH_WEB_SITE . DS . $this->galleryPath . DS . 'gallery-reference' . DS . $gallerie['var_image_reference']);
            }

            $i++;
        }

        $nodelete = count($ides) - $i;

        if (!$_true) {
            echo $i . "  Donnée(s) effacer \n$nodelete donnée(s) n'a pu être effacer. Veuillez changer l'état à Provisoire.";
        }
        else
            echo $i . "  Donnée(s) effacer";
    }

}